#include "communication-device-electrical.h"

CommunicationDeviceElectrical::CommunicationDeviceElectrical(SmartDeviceManager *manager)
    : smartDeviceManager(manager)
{
	handle["add"]    = &CommunicationDeviceElectrical::add;
	handle["remove"] = &CommunicationDeviceElectrical::remove;
	handle["open"]   = &CommunicationDeviceElectrical::open;
	handle["close"]  = &CommunicationDeviceElectrical::close;
	handle["query"]  = &CommunicationDeviceElectrical::query;
}

void CommunicationDeviceElectrical::communication(SmartDevicenetProtocol &netProtocol, NetCommunication *netObj)
{
	//1控制门锁
	string command = netProtocol.getCommand();
	auto   func    = handle[command];
	if(!func)
	{
		return;
	}
	(this->*func)(netProtocol, netObj);
}

void CommunicationDeviceElectrical::add(SmartDevicenetProtocol &netProtocol,
                                        NetCommunication *      netObj)
{
	SmartDeviceDatabase *smartDeviceDatabase = SmartDeviceDatabase::getSmartDeviceDatabase();
	bool                 ok                  = smartDeviceDatabase->addDevice(
        netProtocol.getOperation(),
        netProtocol.getDeviceValue("deviceType"),
        netProtocol.getDeviceValue("deviceNumber"));

	string errmsg = ok ? "" : "error";
	//等待锁那边回复 UDP发送
	replay(netObj, netProtocol.getCommand(), errmsg);
}

void CommunicationDeviceElectrical::remove(SmartDevicenetProtocol &netProtocol,
                                           NetCommunication *      netObj)
{
	SmartDeviceDatabase *smartDeviceDatabase = SmartDeviceDatabase::getSmartDeviceDatabase();
	bool                 ok                  = smartDeviceDatabase->removeDevice(
        netProtocol.getOperation(),
        netProtocol.getDeviceValue("deviceType"),
        netProtocol.getDeviceValue("deviceNumber"));

	string errmsg = ok ? "" : "error";
	//等待锁那边回复 UDP发送
	replay(netObj, netProtocol.getCommand(), errmsg);
}

void CommunicationDeviceElectrical::open(SmartDevicenetProtocol &netProtocol,
                                         NetCommunication *      netObj)
{
	string   command      = netProtocol.getCommand();
	uint32_t deviceType   = netProtocol.getDeviceType();
	uint32_t deviceNumber = netProtocol.getDeviceNumber();
	bool     ok           = false;
	//打开
	ok            = smartDeviceManager->openElectrical(deviceType, deviceNumber);
	string errmsg = ok ? "" : "error";
	//等待锁那边回复
	replay(netObj, command, errmsg);

	return;
}

void CommunicationDeviceElectrical::close(SmartDevicenetProtocol &netProtocol, NetCommunication *netObj)
{
	string   command      = netProtocol.getCommand();
	uint32_t deviceType   = netProtocol.getDeviceType();
	uint32_t deviceNumber = netProtocol.getDeviceNumber();
	bool     ok           = false;
	//打开
	ok            = smartDeviceManager->closeElectrical(deviceType, deviceNumber);
	string errmsg = ok ? "" : "error";
	replay(netObj, command, errmsg);

	return;
}

void CommunicationDeviceElectrical::query(SmartDevicenetProtocol &netProtocol, NetCommunication *netObj)
{

	string   command      = netProtocol.getCommand();
	uint32_t deviceType   = netProtocol.getDeviceType();
	uint32_t deviceNumber = netProtocol.getDeviceNumber();
	bool     ok           = false;
	uint8_t  lockStatu;
	ok            = smartDeviceManager->queryElectrical(deviceType, deviceNumber, lockStatu);
	string errmsg = ok ? "" : "error";
	string data   = lockStatu ? "1" : "0";
	replay(netObj, command, errmsg, data);

	return;
}

void CommunicationDeviceElectrical::replay(NetCommunication *netObj,
                                           const string &    command,
                                           const string &    errmsg,
                                           const string &    data)
{
	SmartDevicenetProtocol netProtocol;
	netProtocol.addOperation("electrical");
	netProtocol.addCommand(command);
	netProtocol.addResult(errmsg, data);
	string repalyJsonData = netProtocol.toString();

	vector<uint8_t> packet;
	packet.assign(repalyJsonData.begin(), repalyJsonData.end());
	cout << "------------udp发送----------------" << endl;
	bool ret = netObj->sendPacket(packet);
	if(ret != true)
	{
		fprintf(stderr, "Fail to sendPacket\n");
		return;
	}

	return;
}
